package com.etc.service;

import com.etc.dao.UserinfoDao;
import com.etc.entity.Userinfo;
import com.etc.util.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {
    @Autowired
    UserinfoDao userinfoDao;

    @Override
    public Userinfo queryByUser(Integer user_no) {
        return userinfoDao.queryByUser(user_no);
    }

    @Override
    @Transactional
    public boolean deleteByUser(Integer user_no) {
        int n = userinfoDao.deleteByUser(user_no);
        return n > 0;
    }

    @Override
    public boolean register(Userinfo userinfo) {
        userinfo.setUser_password(MD5Util.getMd5(userinfo.getUser_password()));
        int n = userinfoDao.register(userinfo);
        return n > 0;
    }

    @Override
    @Transactional
    public boolean updateByUser(Userinfo userinfo) {
        userinfo.setUser_password(MD5Util.getMd5(userinfo.getUser_password()));
        int n = userinfoDao.updateByUser(userinfo);
        return n > 0;
    }

    @Override
    public Userinfo login(String user_acount, String user_password) {
        return userinfoDao.login(user_acount, MD5Util.getMd5(user_password));
    }

    @Override
    public PageInfo<Userinfo> queryAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Userinfo> list = userinfoDao.queryAll();
        PageInfo<Userinfo> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Userinfo queryBy(String user_acount) {
        Userinfo userinfo= userinfoDao.queryBy(user_acount);
        if(userinfo==null){
            userinfo=new Userinfo(0, "0", "0", "0","0");
        }
        return userinfo;
    }
}
